<template>
  <div class="bar-chart-container">
    <h3 class="chart-title">{{ title }}</h3>
    <div class="chart-wrapper">
      <!-- Y轴标签 -->
      <div class="y-axis">
        <div 
          v-for="(tick, index) in yAxisTicks" 
          :key="index"
          class="y-tick"
          :style="{ bottom: `${(tick / maxValue) * 100}%` }"
        >
          <span class="y-tick-label">{{ tick }}</span>
        </div>
      </div>
      
      <!-- 图表区域 -->
      <div class="bar-chart">
        <!-- 网格背景 -->
        <div class="grid-background">
          <div 
            v-for="(tick, index) in yAxisTicks" 
            :key="`grid-${index}`"
            class="grid-line"
            :style="{ bottom: `${(tick / maxValue) * 100}%` }"
          ></div>
        </div>
        
        <!-- 柱状图数据 -->
        <div class="bars-container">
          <div 
            v-for="(item, index) in chartData" 
            :key="index"
            class="bar-item"
          >
            <div class="bar-wrapper">
              <div class="bar-value">{{ item.value }}</div>
              <div 
                class="bar-fill" 
                :style="{
                  height: `${(item.value / maxValue) * 100}%`,
                  animationDelay: `${index * 0.2}s`
                }"
              ></div>
              <!-- 悬停提示 -->
              <div class="bar-tooltip">
                <span class="tooltip-label">{{ item.label }}</span>
                <span class="tooltip-value">{{ item.value }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- X轴标签 -->
      <div class="x-axis">
        <div 
          v-for="(item, index) in chartData" 
          :key="`x-${index}`"
          class="x-tick"
        >
          <span class="x-tick-label">{{ item.label }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface BarData {
  label: string
  value: number
}

interface Props {
  title: string
  data: BarData[]
  barColor?: string
}

const props = withDefaults(defineProps<Props>(), {
  barColor: 'linear-gradient(180deg, #00d4ff, #0099cc)'
})

const chartData = computed(() => props.data)
const maxValue = computed(() => {
  const max = Math.max(...props.data.map(item => item.value))
  // 向上取整到最近的10的倍数，确保图表顶部有空间
  return Math.ceil(max / 10) * 10
})

// 计算Y轴刻度
const yAxisTicks = computed(() => {
  const ticks = []
  const step = maxValue.value / 5 // 5个刻度
  for (let i = 0; i <= 5; i++) {
    ticks.push(Math.round(step * i))
  }
  return ticks
})
</script>

<style scoped>
.bar-chart-container {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(0, 212, 255, 0.3);
  border-radius: 12px;
  padding: 20px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.chart-title {
  font-size: 16px;
  font-weight: 600;
  color: #00d4ff;
  margin: 0 0 20px 0;
  text-align: center;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(0, 212, 255, 0.3);
}

/* 图表包装器 */
.chart-wrapper {
  display: flex;
  flex-direction: column;
  position: relative;
}

/* Y轴样式 */
.y-axis {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 40px;
  width: 40px;
  z-index: 2;
}

.y-tick {
  position: absolute;
  left: 0;
  width: 100%;
  height: 1px;
}

.y-tick-label {
  position: absolute;
  right: 8px;
  top: -8px;
  font-size: 10px;
  color: #888;
  background: rgba(22, 33, 62, 0.8);
  padding: 2px 4px;
  border-radius: 2px;
}

/* 图表区域 */
.bar-chart {
  margin-left: 40px;
  height: 200px;
  position: relative;
  border-left: 1px solid rgba(0, 212, 255, 0.3);
  border-bottom: 1px solid rgba(0, 212, 255, 0.3);
}

/* 网格背景 */
.grid-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
}

.grid-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(255, 255, 255, 0.1);
  border-top: 1px dashed rgba(0, 212, 255, 0.2);
}

/* 柱状图容器 */
.bars-container {
  position: relative;
  height: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: space-around;
  gap: 8px;
  padding: 0 10px;
  z-index: 3;
}

.bar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  min-width: 0;
  height: 100%;
  justify-content: flex-end;
}

.bar-wrapper {
  width: 100%;
  max-width: 30px;
  height: 100%;
  display: flex;
  align-items: center;
  position: relative;
  flex-direction: column;
  justify-content: flex-end;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.bar-wrapper:hover {
  transform: scale(1.1);
  z-index: 10;
}

.bar-fill {
  width: 100%;
  border-radius: 4px 4px 0 0;
  transition: all 0.3s ease;
  position: relative;
  background: linear-gradient(180deg, 
    #00d4ff 0%, 
    #5b8def 25%, 
    #00ff88 50%, 
    #ffff00 75%, 
    #ff6b6b 100%
  );
  background-size: 100% 400%;
  box-shadow: 0 0 10px rgba(0, 212, 255, 0.3);
  animation: barGrow 1.2s ease-out, colorFlow 6s ease-in-out infinite;
}

.bar-wrapper:hover .bar-fill {
  box-shadow: 
    0 0 20px rgba(0, 212, 255, 0.8),
    0 0 40px rgba(91, 141, 239, 0.6),
    0 0 60px rgba(255, 255, 0, 0.4);
  filter: brightness(1.3) saturate(1.2);
  transform: scaleY(1.05);
  transform-origin: bottom;
  animation: barGrow 1.2s ease-out, colorFlowFast 2s ease-in-out infinite;
}

.bar-fill::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, 
    rgba(255, 255, 255, 0.8) 0%, 
    rgba(0, 212, 255, 0.9) 50%, 
    rgba(255, 255, 255, 0.8) 100%
  );
  background-size: 200% 100%;
  border-radius: 2px;
  animation: highlightFlow 3s linear infinite;
}

@keyframes highlightFlow {
  0% {
    background-position: -200% 0%;
  }
  100% {
    background-position: 200% 0%;
  }
}

.bar-value {
  font-size: 10px;
  font-weight: bold;
  color: #00d4ff;
  margin-top: 4px;
  text-shadow: 0 0 8px rgba(0, 212, 255, 0.6);
  text-align: center;
  transition: all 0.3s ease;
}

.bar-wrapper:hover .bar-value {
  transform: scale(1.1);
  color: #fff;
  text-shadow: 0 0 12px rgba(0, 212, 255, 1);
}

/* 悬停提示框 */
.bar-tooltip {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.9);
  border: 1px solid #00d4ff;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 12px;
  color: #fff;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

.bar-tooltip::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: #00d4ff;
}

.bar-wrapper:hover .bar-tooltip {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(-10px);
}

.tooltip-label {
  display: block;
  font-weight: 600;
  color: #00d4ff;
  margin-bottom: 4px;
}

.tooltip-value {
  display: block;
  font-size: 14px;
  font-weight: bold;
  color: #fff;
}

/* X轴样式 */
.x-axis {
  display: flex;
  margin-left: 40px;
  margin-top: 10px;
  padding: 0 10px;
}

.x-tick {
  flex: 1;
  display: flex;
  justify-content: center;
}

.x-tick-label {
  font-size: 11px;
  color: #ccc;
  text-align: center;
  line-height: 1.2;
  word-break: break-all;
  max-width: 50px;
}

/* 动画效果 - 已在上面的 .bar-fill 中定义 */

@keyframes barGrow {
  from {
    height: 0;
  }
  to {
    height: var(--final-height);
  }
}

/* 颜色流动动画 */
@keyframes colorFlow {
  0% {
    background-position: 0% 0%;
  }
  25% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 0% 100%;
  }
  75% {
    background-position: 0% 50%;
  }
  100% {
    background-position: 0% 0%;
  }
}

/* 快速颜色流动动画 - 悬停时使用 */
@keyframes colorFlowFast {
  0% {
    background-position: 0% 0%;
    filter: hue-rotate(0deg);
  }
  33% {
    background-position: 0% 100%;
    filter: hue-rotate(120deg);
  }
  66% {
    background-position: 0% 0%;
    filter: hue-rotate(240deg);
  }
  100% {
    background-position: 0% 100%;
    filter: hue-rotate(360deg);
  }
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .bar-chart {
    height: 180px;
  }
  
  .y-axis {
    bottom: 35px;
  }
  
  .bar-wrapper {
    max-width: 25px;
  }
  
  .x-tick-label {
    font-size: 10px;
    max-width: 40px;
  }
}

@media (max-width: 768px) {
  .bar-chart {
    height: 160px;
  }
  
  .y-axis {
    width: 30px;
    bottom: 30px;
  }
  
  .bar-chart {
    margin-left: 30px;
  }
  
  .x-axis {
    margin-left: 30px;
  }
  
  .bars-container {
    gap: 4px;
  }
  
  .bar-wrapper {
    max-width: 20px;
  }
  
  .x-tick-label {
    font-size: 9px;
    max-width: 30px;
  }
  
  .bar-value {
    font-size: 9px;
  }
  
  .y-tick-label {
    font-size: 9px;
    padding: 1px 2px;
  }
}
</style>
